package com.yan_jiu_sheng.LeetCodeHot100.AC;

/**
 * https://leetcode.cn/problems/kth-smallest-element-in-a-bst/description/?envType=study-plan-v2&envId=top-100-liked
 * 通过
 *
 * @author yulongTian
 * @create 2024-06-14 21:18
 */
public class Test44 {
    public static void main(String[] args) {

    }


    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    private int count = 0;
    private int ans = 0;

    public int kthSmallest(TreeNode root, int k) {
        count = k;
        midOrderSearch(root);
        return ans;
    }

    public void midOrderSearch(TreeNode root) {
        if (root == null) {
            return;
        }
        midOrderSearch(root.left);
        count--;
        if (count == 0) {
            ans = root.val;
        }
        midOrderSearch(root.right);
    }


}
